﻿using Furion.DependencyInjection;
using GWB.Application.System.MenuManage.MenuService;
using GWB.Application.System.RoleManage.Dtos;
using GWB.Core;
using GWB.Core.TableEntity;
using GWB.Supplier;
using Mapster;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GWB.Application.System.RoleManage.RoleService
{
    /// <summary>
    /// 角色管理
    /// </summary>
    public class RoleService : SqlSugarDb, ITransient, IRoleService
    {

        private readonly IMenuService _menuService;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="userService"></param>
        public RoleService( IMenuService menuService)
        {
            _menuService = menuService;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<Result<RoleOutDto>> Add(RoleCreateOrUpdateInputDto inputDto)
        {
            var result = new Result<RoleOutDto>();
            var data = await RoleDal.Create(inputDto.Adapt<Role>());
            result.Data = data.Adapt<RoleOutDto>();
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> Delete(List<long> ids)
        {
            return await RoleDal.Delete(ids);
        }

        /// <summary>
        ///查询
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<PageResult<RoleOutDto>> Page(RolePageInputDto inputDto)
        {
            RefAsync<int> total = 0;
            var data = await Db.Queryable<Role>()
                .OrderBy(x => x.CreateAt, OrderByType.Desc)
                .Select(x => new RoleOutDto() { Id = x.Id.SelectAll() })
                .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);
            return new PageResult<RoleOutDto>()
            {
                Data = data,
                Total = total,
            };
        }
       
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> Update(RoleCreateOrUpdateInputDto inputDto)
        {
            return await RoleDal.Update(inputDto.Adapt<Role>());
        }

        /// <summary>
        /// 编辑角色菜单
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> EditRoleMenu(RoleMenuEditInputDto inputDto)
        {
            return await _menuService.EditAssignMenu(new MenuManage.Dtos.OwnerMenuEditInputDto()
            {
                OwnerId = inputDto.TenantId,
                MenuIds = inputDto.MenuIds,
                Type = 2
            });
        }

        /// <summary>
        /// 获取角色菜单
        /// </summary>
        /// <param name="roelId"></param>
        /// <returns></returns>
        public async Task<List<RoletMenuOutDto>> GetRoleMenus(long roelId)
        {
            var data = await Db.Queryable<OwnerMenu, Menu>((a, b) => new JoinQueryInfos(
                    JoinType.Left, a.MenuId == b.Id))
                .Where((a, b) => a.OwnerId == roelId && a.Type == 2)
                .Select((a, b) => new RoletMenuOutDto() { Id = b.Id.SelectAll() })
                .ToListAsync();
            return data;
        }

    }
}
